package com.hpeng.util;

import java.util.List;

public class Pagination<T> {
	private int firstResult;
	private int maxResult;
	private int currentPage;
	private int count;
	private List<T> dataList;

	public Pagination() {
	}

	public Pagination(int currentPage, int count) {
		this.count = count;
		this.currentPage = currentPage;
	}

	public Pagination(int currentPage, int maxResult, int count) {
		this.currentPage = currentPage;
		this.maxResult = maxResult;	
		this.count = count;
	}

	public List<T> getDataList() {
		return dataList;
	}

	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
	}

	public int getFirstResult() {
		return (getCurrentPage() - 1) * getMaxResult();
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getMaxResult() {
		return (this.maxResult <= 0 ? Constant.PAGE_SIZE : this.maxResult);
	}

	public void setMaxResult(int maxResult) {
		this.maxResult = maxResult;
	}

	public int getCurrentPage() {
		return (this.currentPage <= 0 ? 1 : this.currentPage);
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getPageLink(String uri) {
		if (count == 0) {
			return "<div style=\"text-align:center;\">没有相关记录</div>";
		}
		
		uri = (uri.indexOf("?") != -1) ? (uri + "&") : (uri + "?");
		int totalPage = 0;
		boolean good = (getCount() % getMaxResult() == 0);
		if (good) {
			totalPage = getCount() / getMaxResult();
		} else {
			totalPage = getCount() / getMaxResult() + 1;
		}
		StringBuffer pageLink = new StringBuffer();
		pageLink.append("<div class=\"pagination\">");
		pageLink.append("<span>共&nbsp;"+getCount()+"&nbsp;条纪录, 当前第&nbsp;"+getCurrentPage()+"/"+totalPage+"&nbsp;页, 每页&nbsp;"+getMaxResult()+"&nbsp;条纪录</span>&nbsp;");
		
		if (getCurrentPage() == 1) {
			pageLink.append("<span class=\"disabled\">首页</span>&nbsp;");
			pageLink.append("<span class=\"disabled\">上一页</span>&nbsp;");
			if (totalPage == 1 || totalPage == 0) {
				pageLink.append("<span class=\"disabled\">下一页</span>&nbsp;");
				pageLink.append("<span class=\"disabled\">尾页</span>");
			} else {
				pageLink.append("<a href='" + uri + "page="
						+ (getCurrentPage() + 1) + "'>下一页</a>&nbsp;");
				pageLink.append("<a href='" + uri + "page=" + totalPage
						+ "'>尾页</a>");
			}
		} else if (getCurrentPage() > 1 && getCurrentPage() < totalPage) {
			pageLink.append("<a href='" + uri + "page=1'>首页</a>&nbsp;");
			pageLink.append("<a href='" + uri + "page="
					+ (getCurrentPage() - 1) + "'>上一页</a>&nbsp;");
			pageLink.append("<a href='" + uri + "page="
					+ (getCurrentPage() + 1) + "'>下一页</a>&nbsp;");
			pageLink.append("<a href='" + uri + "page=" + totalPage
					+ "'>尾页</a>");
		} else if (getCurrentPage() == totalPage) {
			pageLink.append("<a href='" + uri + "page=1'>首页</a>&nbsp;");
			pageLink.append("<a href='" + uri + "page="
					+ (getCurrentPage() - 1) + "'>上一页</a>&nbsp;");
			pageLink.append("<span class=\"disabled\">下一页</span>&nbsp;");
			pageLink.append("<span class=\"disabled\">尾页</span>");
		}
		pageLink
				.append("<span class=\"turnto\">转到第<input id=\"turntoPage\" onkeypress=\"if(event.keyCode==13){jumpPage('"
						+ uri
						+ "',this.value,"
						+ totalPage
						+ ");}\" />页</span>");
		pageLink.append("</div>");
		return pageLink.toString();
	}

}
